package com.zhan.ielts.web.controller;

import com.zhan.frame.core.web.domain.Result;
import com.zhan.ielts.domain.TopicQA;
import com.zhan.ielts.domain.vo.TopicDecryptQaVo;
import com.zhan.ielts.service.TopicQaService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/topicQa")
public class TopicQaController {

    private Logger log = LoggerFactory.getLogger(TopicQaController.class);

    @Autowired
    private TopicQaService topicQaService;

    @RequestMapping
    public String list(String topicCode, String partType, String status, Model model) {
        //edit页面重定向参数OK ERROR表示edit结果 进行弹窗提示
        model.addAttribute("status", status);
        if (StringUtils.isEmpty(topicCode)) {
            return "/topicQa/listTopicQa";
        }
        TopicQA query = new TopicQA();
        query.setTopicCode(topicCode);
        query.setPartType(partType);
        List<TopicQA> qaList = topicQaService.selectParam(query);
        //解码，得到含有明文的qaVO
        List<TopicDecryptQaVo> topicDecryptQaVos = topicQaService.buildDecryptQaVOList(qaList);
        model.addAttribute("query", query);
        model.addAttribute("qaList", topicDecryptQaVos);
        return "/topicQa/listTopicQa";
    }

    @RequestMapping(value = "/edit")
    public String edit(String id, Model model) {
        if (StringUtils.isEmpty(id)) {
            return "/topicQa/editTopicQa";
        }
        TopicQA topicQA = topicQaService.queryById(id);
        TopicDecryptQaVo vo = topicQaService.buildDecryptQaVO(topicQA);
        model.addAttribute("topicQa", vo);
        return "/topicQa/editTopicQa";
    }

    @RequestMapping(value = "/update")
    public String update(TopicDecryptQaVo qaVo, Model model) {
        if (StringUtils.isEmpty(qaVo.getTopicCode())) {
            model.addAttribute("status", "问题修改失败！编号不能为空");
            return "redirect:/topicQa";
        }
        if (StringUtils.isEmpty(qaVo.getPartType())) {
            model.addAttribute("status", "问题修改失败！Part不能为空");
            return "redirect:/topicQa";
        }
        if (!"part1".equals(qaVo.getPartType()) && !"part2".equals(qaVo.getPartType()) && !"part3".equals(qaVo.getPartType())) {
            model.addAttribute("status", "问题修改失败！Part只能为part1/part2/part3");
            return "redirect:/topicQa";
        }
        if (null == qaVo.getSort() || qaVo.getSort().intValue() < 1) {
            model.addAttribute("status", "问题修改失败！问题排序不能为空");
            return "redirect:/topicQa";
        }
        if (StringUtils.isEmpty(qaVo.getDecryptQuestion())) {
            model.addAttribute("status", "问题修改失败！问题不能为空");
            return "redirect:/topicQa";
        }
        //vo明文阻断转为字段密文qa对象
        TopicQA topicQa = topicQaService.buildEncryptQa(qaVo);
        model.addAttribute("topicCode", qaVo.getTopicCode());
        if (null == topicQa) {
            model.addAttribute("status", "问题修改失败！");
            return "redirect:/topicQa";
        }
        try {
            topicQaService.updateOrInsert(topicQa);
            model.addAttribute("status", "问题修改成功！");
        } catch (Exception e) {
            model.addAttribute("status", "问题修改失败！");
        }
        return "redirect:/topicQa";
    }

}